# routines to add nodes
addnode()
{
    lockfile $1.lock
    echo "$2" >> $1
    rm -f $1.lock
}

check_stop_condition()
{
    stopping=$1
    logging=$2
    if [[ -e ${stopping} ]]; then
        echo -e "[$(date)] File \"${stopping}\" detected. Exiting... (tasks will not be interrupted)" >> ${logging}
        exit
    fi
}

# Directories
dirreno="/home/nucave/workdir/lattice/RENORMALIZATION/reno"

dir="$dirreno/NF4/Z4F/"
dirinput="$dirreno/NF4/Z4F/INPUT"

dirdata="/home/nucave/workdir/lattice/RENORMALIZATION/"

# Fermionic Action
Nf=4
action="TwistedMass"



########################################################
#### READ TASK
######################################################



log="log.Calc_D.$(date +%d%b%Y-%Hh%Mm)"
script="launch_D_onemomenta.sh"
executable="Calc_D_oneP"
list="todo_list.Calc_D"
nodes="nodes.Calc_D"
stopping=".parate"


while [[ -s ${list} ]] 
do    ##### BEGIN DO OVER ELEMENT IN LIST
        	check_stop_condition ${stopping} ${log}
                if [[ -s ${nodes} ]]; then

                                # choosing an empty slot
                                lockfile ${nodes}.lock
                                slot=$(cat ${nodes}|sed -e '1!D')
                                sed -e '1D' ${nodes} > ${nodes}.temp
                                mv ${nodes}.temp ${nodes}
                                rm -f ${nodes}.lock

                                # reading the task
                                lockfile ${list}.lock
                                task=$(cat ${list} | sed -e '1!D')
                                sed -e '1D' ${list} > ${list}.temp
                                mv ${list}.temp ${list}
                                rm -f ${list}.lock

                                if [[ -n ${task} ]]; then
					beta100=$(echo ${task} | awk  '{print $1}') 
					e=$(echo ${task} | awk  '{print $2}')		
					pm=$(echo ${task} | awk  '{print $3}')	
		                        imom=$(echo ${task} | awk  '{print $4}')
					Nmass=$(echo ${task} | awk  '{print $5}')
					Nrc=$(echo ${task} | awk  '{print $6}')
                                    #    imass=$(echo ${task} | awk  '{print $5}')
                                    #    irc=$(echo ${task} | awk  '{print $6}')
                                        jmom=`printf %03d $imom`
                                else
                                    addnode ${nodes} ${slot}
                                    slot=""
                                    sleep 20s
                                fi

                else
                                slot=""
                                sleep 20s
                fi
	
###############################
## END READ TASK
##################################

#####################################
## INPUT VARIBALES
#########################################

	beta=$(echo "scale=2; $beta100/100" | bc -l)

	#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	#@@@@ gamma base & momentum order
	#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	if [ $beta100 -eq 190 ] || [ $beta100 -eq 210 ]
	then
	gamma="TWISTED"
	momentum_order="TYZX"
	convention="TW"
	fi

	if [ $beta100 -eq 195 ]
	then
	gamma="APE"
	momentum_order="TYZX"
	convention="APE"
	fi

	#@@@@@@@@@@@@@@@@@@@@@@@@@
	# L^3xT=V
	#@@@@@@@@@@@@@@@@@@@@@@@@@

	if [ $beta100 -eq 190 ]
	then
	 L=24
	 T=48
	fi
	if [ $beta100 -eq 195 ]
	then
	 L=24
	 T=48
	fi
	if [ $beta100 -eq 210 ]
	then
	 L=32
 	 T=64
	fi

                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
                #@@  Configurations to be read:
                #@@  conf=conf_ini+i*jump with 
                #@@      i=0,1,...Nconf-1
                #@@      Nconf=Nex*Nclust
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

		Nclust=16
		
		epm=${e}${pm}
                if [ $beta100 -eq 190 ]
                then

                        if [ $epm = "A1m" ] ; then conf_ini=129;  jump=6; Nex=15; fi
                        if [ $epm = "A1p" ] ; then conf_ini=499 ; jump=6; Nex=15; fi

                        if [ $epm = "A3m" ] ; then conf_ini=198 ; jump=6 ; Nex=15; fi
                        if [ $epm = "A3p" ] ; then conf_ini=499 ; jump=6 ; Nex=15; fi

                        if [ $epm = "A4m" ] ; then conf_ini=499 ; jump=6; Nex=15; fi
                        if [ $epm = "A4p" ] ; then conf_ini=499 ; jump=4; Nex=20; fi


                fi

                if [ $beta100 -eq 195 ]
                then
                        jump=4

                        if [ $e -eq 1 ] ; then  Nex=19 ;fi
                        if [ $e -eq 2 ] ; then  Nex=22 ;fi
                        if [ $e -eq 3 ] ; then  Nex=19 ;fi
                        if [ $e -eq 4 ] ; then  Nex=14 ;fi
                        if [ $e -eq 7 ] ; then  Nex=23 ;fi
                        if [ $e -eq 8 ] ; then  Nex=19 ;fi

                        if [ $epm = "1m" ] ; then conf_ini=118 ;fi
                        if [ $epm = "1p" ] ; then conf_ini=102 ;fi

                        if [ $epm = "2m" ] ; then conf_ini=105 ;fi
                        if [ $epm = "2p" ] ; then conf_ini=384 ;fi

                        if [ $epm = "3m" ] ; then conf_ini=100 ;fi
                        if [ $epm = "3p" ] ; then conf_ini=241 ;fi
                        if [ $epm = "4m" ] ; then conf_ini=459 ;fi
                        if [ $epm = "4p" ] ; then conf_ini=172 ;fi

                        if [ $epm = "7m" ] ; then conf_ini=1454 ;fi
                        if [ $epm = "7p" ] ; then conf_ini=102 ;fi

                        if [ $epm = "8m" ] ; then conf_ini=127 ;fi
                        if [ $epm = "8p" ] ; then conf_ini=326 ;fi
                fi
                if [ $beta100 -eq 210 ]
                then

                        if [ $epm = "2m" ] ; then jump=4; Nex=12; conf_ini=260; fi

                        if [ $epm = "3p" ] ; then jump=1; Nex=15; conf_ini=100; fi
                        if [ $epm = "3m" ] ; then jump=1; Nex=22; conf_ini=100; fi
                        if [ $epm = "4p" ] ; then jump=1; Nex=13; conf_ini=100; fi
                        if [ $epm = "4m" ] ; then jump=1; Nex=23; conf_ini=100; fi
                        if [ $epm = "5p" ] ; then jump=1; Nex=14; conf_ini=100; fi
                        if [ $epm = "5m" ] ; then jump=1; Nex=18; conf_ini=100; fi

                fi
	

	#@@@@@@@@@@@@@@@@@@
	# Momenta
	#@@@@@@@@@@@@@@@@@@@@@

	if [ $beta100 -eq 190 ]
	then
	Nmom=492
	Nmom_eq=135
	fi

	if [ $beta100 -eq 195 ]
	then
	Nmom=140
	Nmom_eq=55

	fi
	if [ $beta100 -eq 210 ]
	then
	Nmom=492
	Nmom_eq=135
	fi


	echo "1 0 0 0" > $dirinput/mom_${beta100}.dat # boundary conditions
	echo "2" >> $dirinput/mom_${beta100}.dat # Nindexmom
	if [ $Nmom -eq 140 ]
	then
	echo "0 3 0 2 0 2 0 2" >> $dirinput/mom_${beta100}.dat
	echo "4 7 2 3 2 3 2 3" >> $dirinput/mom_${beta100}.dat
	fi
	if [ $Nmom -eq 492 ]
	then
	echo "0 3 0 2 0 2 0 2" >> $dirinput/mom_${beta100}.dat
	echo "4 9 2 5 2 5 2 5" >> $dirinput/mom_${beta100}.dat
	fi



                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
                #@@  kappa_sea                       
                #@   (only for name construction)
                #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#

                if [ $beta100 -eq 195 ]
                then
                        if [ $epm = "1m" ] ; then kappa_sea="0.160754" ;fi
                        if [ $epm = "1p" ] ; then kappa_sea="0.162145" ;fi

                        if [ $epm = "2m" ] ; then kappa_sea="0.161229" ;fi
                        if [ $epm = "2p" ] ; then kappa_sea="0.160826" ;fi

                        if [ $epm = "3m" ] ; then kappa_sea="0.161229" ;fi
                        if [ $epm = "3p" ] ; then kappa_sea="0.160826" ;fi

                        if [ $epm = "4m" ] ; then kappa_sea="0.161095" ;fi
                        if [ $epm = "4p" ] ; then kappa_sea="0.16087" ;fi

                        if [ $epm = "7m" ] ; then kappa_sea="0.160681" ;fi
                        if [ $epm = "7p" ] ; then kappa_sea="0.161925" ;fi

                        if [ $epm = "8m" ] ; then kappa_sea="0.160681" ;fi
                        if [ $epm = "8p" ] ; then kappa_sea="0.161925" ;fi

                fi
                if [ $beta100 -eq 210 ]
                then
                        if [ ${epm} = "2m" ] ; then kappa_sea="0.156157" ; fi

                        if [ ${epm} = "3m" ] ; then kappa_sea="0.156209" ; fi
                        if [ ${epm} = "3p" ] ; then kappa_sea="0.156017" ; fi
                        if [ ${epm} = "4m" ] ; then kappa_sea="0.156250" ; fi
                        if [ ${epm} = "4p" ] ; then kappa_sea="0.155983" ; fi
                        if [ ${epm} = "5m" ] ; then kappa_sea="0.156291" ; fi
                        if [ ${epm} = "5p" ] ; then kappa_sea="0.155949" ; fi
                fi
                if [ $beta100 -eq 190 ]
                then
                        if [ ${epm} = "A1p" ] ; then kappa_sea="0.163206" ; fi

                        if [ ${epm} = "A3p" ] ; then kappa_sea="0.164537" ; fi
                        if [ ${epm} = "A3m" ] ; then kappa_sea="0.162260" ; fi
                        if [ ${epm} = "A4p" ] ; then kappa_sea="0.164817" ; fi
                        if [ ${epm} = "A4m" ] ; then kappa_sea="0.162113" ; fi

                fi

	#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        # IO dirs
	#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        if [ $convention = "APE" ]
        then
             dir_input="$dirdata/DATA/INPUT-APE/${action}Nf${Nf}/b$beta100/${e}${pm}/"
             dir_output="$dirdata/DATA/OUTPUT-APE/${action}Nf${Nf}/b$beta100/Z4F/${e}${pm}/"
        else
             dir_input="$dirdata/DATA/INPUT/${action}Nf${Nf}/b$beta100/${e}${pm}/"
             dir_output="$dirdata/DATA/OUTPUT/${action}Nf${Nf}/b$beta100/Z4F/${e}${pm}/"
        fi

#	mkdir -p -v $dir_output/p$jmom 
	mkdir -p -v $dir_output/0001/p$jmom 
	mkdir -p -v $dir_output/1110/p$jmom 



	#@@@@@@@@@@@@@@@@
	#@@ input arg
	#@@@@@@@@@@@@@@@

	# Esto es comun a todo un ensemble
        args="-gamma=${gamma} -momentum_order=${momentum_order} -L=$L -T=$T -dir_input=$dir_input -dir_output=$dir_output -kappa_sea=$kappa_sea -conf_ini=${conf_ini} -jump=${jump} -Nclust=${Nclust} -Nex=${Nex}  -beta100=${beta100} "
	# save args to be read
        echo $args > $dir/EXECUTABLES/args_${epm}


#################################
# launch the task
#################################

         if [[ -n ${slot} ]]; then

               inode=$(echo ${slot} | awk -F"cpu" '{print $1}')
               icpu=$(echo ${slot} | awk -F"${inode}" '{print $2}')

		echo "ssh ${inode} \"sh ${dir}/SCRIPTS/Calc_D/${script} ${epm} $imom $Nmass $Nrc $log $slot $nodes &> $dir/LOGS/Calc_D_${beta100}_${epm}_${jmom}_${slot}.log "
               ssh ${inode} "sh ${dir}/SCRIPTS/Calc_D/${script} ${epm} $imom $Nmass $Nrc $log $slot $nodes &> $dir/LOGS/Calc_D_${beta100}_${epm}_${jmom}_${slot}.log  2>&1 & "

                lockfile ${log}.lock
                echo -e " ${slot}  beta=${beta} -- ensemble=${epm} --- p:${jmom} --- In_progress" >> ${log}
                rm -f ${log}.lock
                sleep 20s
               fi
 
done # END DO OVER ELEMENT IN LIST	
